package com.gps.skyrc.activity.base;


import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.app.FragmentActivity;
import android.view.Window;
import android.view.WindowManager;

import com.gps.skyrc.tool.EventBusUtils;
import com.gps.skyrc.tool.LogoutUtils;
import com.gps.skyrc.tool.SystemBarTintManager;

import java.util.ArrayList;

import butterknife.ButterKnife;
import butterknife.Unbinder;


public abstract class BaseActivity extends FragmentActivity {
    /**
     * 加载布局文件
     */
    protected abstract void getLayout();

    /**
     * 设置标题
     */
    protected abstract void setTitleBar();

    /**
     * 其他事件
     */
    protected abstract void init();

    protected Activity mContext;
    private Unbinder mUnbinder;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getLayout();
        mContext = this;
        mUnbinder = ButterKnife.bind(this);
        setTitleBar();
        init();
        if (isRegisterEventBus()) EventBusUtils.register(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (isRegisterEventBus()) EventBusUtils.unregister(this);
        mUnbinder.unbind();
    }

    /**
     * 是否注册事件分发
     * @return true绑定EventBus事件分发，默认不绑定，子类需要绑定的话复写此方法返回true.
     */
    protected boolean isRegisterEventBus() {
        return false;
    }

    /*public static DeviceInfo updateDevice(DeviceInfo mDevice) {
        List<DeviceInfo> deviceInfos = Dev.getInstance().getInfos();
        for (DeviceInfo info : deviceInfos) {
            if (mDevice.getMac().equals(info.getMac())) {
                return info;
            }
        }
        return mDevice;
    }*/

    /**
     * 通过Class跳转
     **/
    protected void startActivity(Class<?> cls) {
        Intent intent = new Intent();
        intent.setClass(this, cls);
        startActivity(intent);
        LogoutUtils.enterAnimation(this);
    }

    /**
     * 默认退出
     **/
    protected void defaultFinish() {
        super.finish();
    }

    /**
     * 含有Bundle通过Class跳转
     **/
    public void startActivity(Class<?> cls, Bundle bundle) {
        Intent intent = new Intent();
        intent.putExtras(bundle);
        intent.setClass(this, cls);
        startActivity(intent);
        LogoutUtils.enterAnimation(this);
    }

    /**
     * 含有Action跳转
     **/
    protected void startActivity(String action, String uriMsg) {
        Uri uri = Uri.parse(uriMsg);
        Intent intent = new Intent(action, uri);
        startActivity(intent);
        LogoutUtils.enterAnimation(this);
    }

    /**
     * 通过Class跳转，传int
     */
    protected void startActivity(Class<?> cls, int id) {
        Intent intent = new Intent();
        intent.putExtra("id", id);
        intent.setClass(this, cls);
        startActivity(intent);
        LogoutUtils.enterAnimation(this);
    }

    /**
     * 通过Class跳转，传boolean
     */
    protected void startActivity(Class<?> cls, boolean flag) {
        Intent intent = new Intent();
        intent.putExtra("flag", flag);
        intent.setClass(this, cls);
        startActivity(intent);
        LogoutUtils.enterAnimation(this);
    }

    /**
     * 通过Class跳转，传String
     */
    protected void startActivity(Class<?> cls, String string) {
        Intent intent = new Intent();
        intent.putExtra("string", string);
        intent.setClass(this, cls);
        startActivity(intent);
        LogoutUtils.enterAnimation(this);
    }




    /*public void startActivity(Class<?> cls, DeviceInfo device) {
        Intent intent = new Intent();
        Bundle bundle = new Bundle();
        bundle.putParcelable(Config.DEVICE, device);
        intent.putExtras(bundle);
        intent.setClass(this, cls);
        startActivity(intent);
    }

    public void startActivity(Class<?> cls, List<BleDevice> mScanDevices) {
        Intent intent = new Intent();
        Bundle bundle = new Bundle();
        bundle.putParcelableArrayList(Config.DEVICES_LIST_SCAN, (ArrayList<? extends Parcelable>) mScanDevices);
        intent.putExtras(bundle);
        intent.setClass(this, cls);
        startActivity(intent);
    }

    public void startActivity(Class<?> cls, BleDevice mScanDevice) {
        Intent intent = new Intent();
        Bundle bundle = new Bundle();
        bundle.putParcelable(Config.DEVICE_SCAN, mScanDevice);
        intent.putExtras(bundle);
        intent.setClass(this, cls);
        startActivity(intent);
    }

    public void startActivity(Class<?> cls, DeviceInfo device, List<BleDevice> mScanDevices, int requestCode) {
        Intent intent = new Intent();
        Bundle bundle = new Bundle();
        bundle.putParcelable(Config.DEVICE, device);
        bundle.putParcelableArrayList(Config.DEVICES_LIST_SCAN, (ArrayList<? extends Parcelable>) mScanDevices);
        intent.putExtras(bundle);
        intent.setClass(this, cls);
        startActivityForResult(intent, requestCode);
    }

    public void resultDeviceList(Intent intent, List<DeviceInfo> devices, int resultCode) {
        Bundle bundle = new Bundle();
        bundle.putParcelableArrayList(Config.DEVICES_LIST, (ArrayList<? extends Parcelable>) devices);
        intent.putExtras(bundle);
        setResult(resultCode, intent);
    }

    public void resultDevice(Intent intent, DeviceInfo device, int resultCode) {
        Bundle bundle = new Bundle();
        bundle.putParcelable(Config.DEVICE, device);
        intent.putExtras(bundle);
        setResult(resultCode, intent);
    }*/

    /**
     * 修改状态栏颜色 6.0+只是把状态栏改成透明
     * @param activity
     * @param color
     */
    protected void setTint(Activity activity, int color) {
        if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            Window window = activity.getWindow();
            window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, WindowManager.LayoutParams
                    .FLAG_TRANSLUCENT_STATUS);
            // 创建状态栏的管理实例
            SystemBarTintManager tintManager = new SystemBarTintManager(activity);
            tintManager.setStatusBarTintColor(activity.getResources().getColor(color));
        }
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        onDefaultFinish();
    }

    /**
     * 默认退出
     **/
    public void onDefaultFinish() {
        onDefaultFinish(0);
    }

    public void onDefaultFinish(int status) {
        finish();
        if (status == 0)
            LogoutUtils.exitAnimation(this);
        else
            LogoutUtils.enterAnimation(this);
    }

}
